/**
 * Filename: DetailAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/15 10:08
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.ImageLocalEditZoomBigActivity;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.ArrayList;
import java.util.List;

public class ImagelocalEditAdapter extends
        RecyclerView.Adapter<ImagelocalEditAdapter.ViewHolder> {
    private Context context;
    private LayoutInflater mInflater;
    private List<String> mDatas;

    public ImagelocalEditAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        mDatas = SelectedImg.mSelectedImage;
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        SimpleDraweeView mImg;
        ImageView mSelect;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = mInflater.inflate(R.layout.yn_imagelocaledit_item,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.mImg = (SimpleDraweeView) view
                .findViewById(R.id.iv_album_grid_item_image);
        viewHolder.mSelect = (ImageView) view
                .findViewById(R.id.id_item_select);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
        final String url = mDatas.get(i);
        if (url.startsWith("http")) {
            Uri uri = Uri.parse(url);
            ImageOptHelper.showFrescoNative(uri, viewHolder.mImg, context);
        } else {
            Uri uri = Uri.parse("file://" + url);
            ImageOptHelper.showFrescoNative(uri, viewHolder.mImg, context);
        }
        if (mOnItemClickLitener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(viewHolder.itemView, i);
                }
            });
        }
        viewHolder.mSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (SelectedImg.mSelectedImage.size()==1)
                    return;
                if (SelectedImg.mSelectedImage.contains(url)) {
                    SelectedImg.mSelectedImage.remove(url);
                }
                notifyDataSetChanged();
            }
        });
        viewHolder.mImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> zoomlist = new ArrayList<String>();
                zoomlist.clear();
                zoomlist.addAll(mDatas);
                Intent zoomintent = new Intent(context, ImageLocalEditZoomBigActivity.class);
                zoomintent.putStringArrayListExtra("picUrlsList", zoomlist);
                zoomintent.putExtra("position", i);
                context.startActivity(zoomintent);
            }
        });
        viewHolder.setIsRecyclable(false);
    }

}
